﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCT.Framework;
using VISS.API.Users;

namespace VISS.Web.BackEnd.Modules.User {

    public partial class UserSearchPage : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {

            string searchField = ConvertUtils.ToString(Request.QueryString["tukhoa"]);
            EUserSearchBy searchBy = (EUserSearchBy)ConvertUtils.ToByte(Request.QueryString["timtheo"], 0);
            int page = ConvertUtils.ToInt(Request.QueryString["page"], 1);

            if ((int)searchBy != 0) {
                List<UserAdminEnt> searchResult = new List<UserAdminEnt>();
                long totalRecords = 0;
                IUserAdmin objUserAdmin = StructureMapFactory.GetInstance<IUserAdmin>();
                switch (searchBy) {
                    case EUserSearchBy.UserKey:
                        totalRecords = 1;
                        UserAdminEnt result = objUserAdmin.GetByUserKey_Admin(searchField);
                        if (result != null) {
                            searchResult.Add(result);
                        }
                        break;
                    case EUserSearchBy.Email:
                        searchResult = objUserAdmin.SearchByEmail(searchField, page, AppSettings.RecordPerPage, out totalRecords);
                        break;
                    case EUserSearchBy.MobiPhone:
                        searchResult = objUserAdmin.SearchByMobiPhone(searchField, page, AppSettings.RecordPerPage, out totalRecords);
                        break;
                }

                idPaging.TotalRecord = totalRecords;
                idPaging.PageSize = AppSettings.RecordPerPage;
                idPaging.CurrentPage = page;
                idPaging.TargetPage = "/users/tim_kiem";
                idPaging.QueryString = "tukhoa=" + searchField.Replace("%", "%25") + "&timtheo=" + (int)searchBy + "&";

                idUserList.UserList = searchResult;
            }
        }
    }
}
